package malloc;

import java.util.Arrays;
import java.util.HashMap;

//有多少数字小于当前数字
public class basic1365 {

    public int[] smallerNumbersThanCurrent(int[] nums) {

//        Arrays.sort(nums); //TODO:这样子写的话initial也跟着变化顺序  掌握Arrays的用法 先copy 然后sort
//                                                                 然后掌握HashMap的用法
        int[] temp = Arrays.copyOf(nums, nums.length);
        Arrays.sort(temp);
        int[] newnums = new int[nums.length];
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(temp[0],0 );
        for (int i = 1; i < temp.length ; i++) {
            if (temp[i]>temp[i-1]){
                map.put(temp[i],i );
            }
        }

        for (int i = 0; i < nums.length; i++) {
            newnums[i]=map.get(nums[i]);
        }

//        newnums=nums;
        return  newnums;
    }

    public static void main(String[] args) {

        basic1365 basic1365 = new basic1365();
        int[] ints = basic1365.smallerNumbersThanCurrent(new int[]{4, 5, 2, 1});
        for (int a:ints) {
            System.out.println(a);
        }

    }

}
